<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="ESP.VO.Role"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <jsp:include page="../include.jsp" flush="true"/>
	<script type="text/javascript">
	var grid;
	var url;
	/**/
	$(function() {
		grid = $('#user_info').datagrid();
		$('#btn-save,#btn-cancel').linkbutton();
	});
	
	function clearGroup(){
		var rows = $('#user_info').datagrid('getSelected');
		if(!rows || rows.stu_group_id == null){
			show('清除的小组信息不存在!');
			return false;
		}
		if(rows.user_role_id == 3 || rows.user_role_id == 6){
			show('不能清除组长的小组信息！');
			return false;
		}
		$.messager.confirm('提示','确定清除'+rows.user_name+'的选组信息吗?',function(result){  
            if (result){  
                $.post('a03!clearGroup?stu_id='+rows.user_id,function(message){  
                    $('#user_info').datagrid('reload');
                    show(message.returnMsg);
                });
            } 
            reject();
        });
	}
	//选组员
	function confirmGroupMember(){
		var rows = $('#user_info').datagrid('getSelected');
		if(!rows){
			show('请先选择一行数据!');
			return false;
		}
		if(rows.stu_group_id != "" && rows.stu_group_id != null){
			show(rows.user_name+'已经是'+rows.stu_group_id+'小组的组员!可由学委重新给'+rows.user_name+'分配小组!');
			return false;
		}
		$.messager.confirm('提示', '确定选择'+rows.user_name+'作为组员?', function(result){
			if(result){
				$.post('a03!conformTeamMember',
				{
					user_id:user_id,
					stu_id:rows.user_id
				},
				function(message){
					show(message.returnMsg);
					if(message.returnFlag == "Y"){
						 $('#user_info').datagrid('reload');
					}
				});
			}
		});
	}
	//分配角色
	function confirmRole(roleId) {//
		var rows = $('#user_info').datagrid('getSelected');
		if(!rows){
			show('请先选择一行数据!');
			return false;
		}
		if(rows.user_role_id == '6'){
			show('无法给'+rows.user_name+'分配该角色!');
			return false;
		}
		if(roleId == rows.user_role_id){
			show(rows.user_name+"已经是"+rows.role_name+'!');
			return false;
		}
		if(roleId == '3' && rows.stu_group_id != ""){//分配组长
			$.post('a03!checkHaveGroupLeader',
				{
					user_role_id:user_role_id,
					stu_group_id:rows.stu_group_id
				},
				function(message){  
		       		if(message.returnFlag == 'N'){
		       			show(message.returnMsg);
		       			return false;
		       		}else{
		       			confirmRolePublic(roleId);
		       		}
				}
			);
		}
		if(roleId == '2' && (user_role_id == '1' || user_role_id == '5')){//由管理员或者教师分配学委
			$.post('a03!checkHaveLeaderSuper',
				{
					user_role_id:user_role_id,
					stu_faculty_id:rows.stu_faculty_id,
					stu_major_id:rows.stu_major_id,
					stu_grade:rows.stu_grade,
					stu_class:rows.stu_class
				},
				function(message){  
		       		if(message.returnFlag == 'N'){
		       			$.messager.confirm('提示', message.returnMsg+'是否更改该班学委人选?', function(result){
		       				if(result){
		       					$.post('a03!clearXuewei',{
		       						stu_grade:rows.stu_grade,
									stu_faculty_id:rows.stu_faculty_id,
									stu_major_id:rows.stu_major_id,
									stu_class:rows.stu_class
		       					},function (message){
		       						if(message.returnFlag == 'N'){
		       							show(message.returnMsg);
		       						}else{
		       							$.post('a03!confirmRole',
											{
												user_role_id:user_role_id,
												stu_id:rows.user_id,
												stu_role_id:roleId
											},
											function(message){  
								        		show(message.returnMsg);
								        		$('#user_info').datagrid('reload');
											}
										);
		       						}
		       					});
		       				}
		       			});
		       		}else{
		       			confirmRolePublic(roleId);
		       		}
				}
			);
		}
	}
	function confirmRolePublic(roleId){
		var rows = $('#user_info').datagrid('getSelected');
		$.messager.confirm('提示','确定给'+rows.user_name+'分配该角色吗?',function(result){
			if (result){
				$.post('a03!confirmRole',
					{
						user_role_id:user_role_id,
						stu_id:rows.user_id,
						stu_role_id:roleId
					},
					function(message){  
		        		show(message.returnMsg);
		        		$('#user_info').datagrid('reload');
					}
				);
			}
		});
	}
	function query(){
		var params = $('#user_info').datagrid('options').queryParams;
        var fields =$('#query-form').serializeArray();
        $.each( fields, function(i, field){
            params[field.name] = field.value;
        });   
        $('#user_info').datagrid('reload');
	}
	function reject(){
		$('#user_info').datagrid('clearSelections');
	} 
</script>
	<script type="text/javascript">
		$(function(){
			var lastIndex;
			$('#user_info').datagrid({
				width:'auto',
				height:'auto',
				method:'post',
				fit:true,
				nowrap: false,
				fitColumns:'true',
				striped: true,
				url:'a03!queryStudent?user_role_id='+user_role_id+'&user_id='+user_id,
				sortName: 'user_id',
				idField:'user_id',
				sortOrder: 'desc',//列排序
				columns:[[
					{field:'ck',checkbox:true,width:2}, //显示复选框
					{field:'user_id',title:'学号',align:'center',width:120},
					{field:'user_name',title:'姓名',align:'center',width:120},
					{field:'user_role_id',title:'角色',align:'center',width:120,hidden:true},
					{field:'role_name',title:'角色',align:'center',width:120},
					{field:'stu_grade',title:'年级',align:'center',width:120},
					{field:'stu_faculty_id',title:'学院',align:'center',width:120,hidden:true},
					{field:'faculty_name',title:'学院',align:'center',width:120},
					{field:'stu_major_id',title:'专业',align:'center',width:120,hidden:true},
					{field:'major_name',title:'专业',align:'center',width:120},
					{field:'stu_class',title:'班级',align:'center',width:120},
					{field:'stu_group_id',title:'小组',align:'center',width:120}
				]],
				rownumbers:true,
				singleSelect:true,
				toolbar:toolbar,
				onLoadSuccess:function(data){  
               		$('#user_info').datagrid('clearSelections');
            	}
			});
		});
		
	</script>
  </head>
  <body>
  	<div class="easyui-layout" data-options="fit:true">
		<div data-options="region:'north',split:false,border:true" title="" style="height:40px">
			<form id="query-form" method="post" style="padding-left:10px;"> 
		  		学号<input type="text" class="easyui-validatebox" name="stu_id" style="width:90px;">&nbsp;
		  		姓名<input type="text" class="easyui-validatebox" name="stu_name" style="width:90px;"></input>&nbsp;
				角色<input type="text" class="easyui-combobox" name="stu_role_id" style="width:90px;"
										data-options="
											url:'c01!queryRole',
											mode:'remote',
											valueField:'ROLE_ID',
											textField:'ROLE_NAME',
											panelHeight:'auto'
										">&nbsp;
				年级<input type="text" class="easyui-validatebox" name="stu_grade" style="width:50px;">&nbsp;
				所属学院<input class="easyui-combobox" name="stu_faculty_id" style="width:90px;"
										data-options="
												url:'c01!queryFaculty',
												mode:'remote',
												valueField:'FACULTY_ID',
												textField:'FACULTY_NAME',
												panelHeight:'auto'
									">&nbsp;
				所属专业<input class="easyui-combobox" name="stu_major_id" style="width:90px;"
										data-options="
												url:'c01!queryMajor',
												mode:'remote',
												valueField:'MAJOR_ID',
												textField:'MAJOR_NAME',
												panelHeight:'auto'
									">&nbsp;
				班级<input type="text" class="easyui-validatebox" name="stu_class" style="width:50px;">&nbsp;
				是否分组<select class="easyui-combobox" name="stu_groupstate" style="width:80px;" data-options="panelHeight:'auto'">
							<option value="">不限</option>
							<option value="Y">已被分组</option>
							<option value="N">未被分组</option>
						</select>&nbsp;
				<a href="javascript:void(0)"  onclick="query()" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-search'">查询</a>
		  	</form>
		</div>
		<div data-options="region:'center',border:false" title="课题信息列表">
			<table id="user_info"></table>
		</div>
	</div>
	<!-- ======================================================== -->
	<script type="text/javascript" src="./jquery-easyui-1.3.2/easyloader.js"></script>
	<script type="text/javascript">
		if(user_role_id == 1 || user_role_id == 5){//教师 管理员
			var toolbar = [{
				text:'分配学委',
				iconCls:'icon-edit',
				handler:function(){confirmRole('2')}
			},{
				text:'分配组长',
				iconCls:'icon-edit',
				handler:function(){confirmRole('3')}
			},{
				text:'清空选组信息',
				iconCls:'icon-edit',
				handler:function(){clearGroup()}
			},{
				text:'返回',
				iconCls:'icon-undo',
				handler:function(){reject()}
			},{
				text:'帮助',
				iconCls:'icon-help',
				handler:function(){reject()}
			}];
		}else if(user_role_id == 6){//学委兼组长
			var toolbar = [{
				text:'分配组长',
				iconCls:'icon-edit',
				handler:function(){confirmRole('3')}
			},{
				text:'选组员',
				iconCls:'icon-edit',
				handler:function(){confirmGroupMember()}
			},{
				text:'清空选组信息',
				iconCls:'icon-edit',
				handler:function(){clearGroup()}
			},{
				text:'返回',
				iconCls:'icon-undo',
				handler:function(){reject()}
			},{
				text:'帮助',
				iconCls:'icon-help',
				handler:function(){reject()}
			}];
		}else if(user_role_id == 2){//学委
			var toolbar = [{
				text:'分配组长',
				iconCls:'icon-edit',
				handler:function(){confirmRole('3')}
			},{
				text:'清空选组信息',
				iconCls:'icon-edit',
				handler:function(){clearGroup()}
			},{
				text:'返回',
				iconCls:'icon-undo',
				handler:function(){reject()}
			},{
				text:'帮助',
				iconCls:'icon-help',
				handler:function(){reject()}
			}];
		}else if(user_role_id == 3){//组长
			var toolbar = [{
				text:'选组员',
				iconCls:'icon-edit',
				handler:function(){confirmGroupMember()}
			},{
				text:'返回',
				iconCls:'icon-undo',
				handler:function(){reject()}
			},{
				text:'帮助',
				iconCls:'icon-help',
				handler:function(){reject()}
			}];
		}else{
			var toolbar = [{
				text:'返回',
				iconCls:'icon-undo',
				handler:function(){reject()}
			},{
				text:'帮助',
				iconCls:'icon-help',
				handler:function(){reject()}
			}];
		}
	</script>
  </body>
</html>
